import Vue from 'vue'
import Vuex from 'vuex'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/common'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userId: '',
    agentConfigStatus: false,
    token: getToken(),
  },
  mutations: {
      SET_TOKEN: (state, token) => {
          state.token = token
      },
  },
  actions: {
      // 登录
      Login({ commit }, userInfo) {
          const userId = userInfo.userId;
          const corpId = userInfo.corpId;
          return new Promise((resolve, reject) => {
              login(userId, corpId).then(res => {
                  console.debug(res);
                  setToken(res.data);
                  commit('SET_TOKEN', res.data);
                  resolve()
              }).catch(error => {
                  reject(error)
              })
          })
      },
  },
  modules: {},
})
